﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VideoSpliter.Util
{
    public class ThumbnailManager
    {
        private static readonly string _root;

        private static Dictionary<string, ObservableCollection<string>> _dict = new Dictionary<string, ObservableCollection<string>>();

        private static int counter = 0;

        static ThumbnailManager()
        {
            _root = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "thumbnail");
            var di = Directory.CreateDirectory(_root);
            di.Attributes = FileAttributes.Hidden;
        }

        public static string GenerateFileName(string path)
        {
            var hashCode = Path.Combine(Path.GetDirectoryName(path), Path.GetFileName(path)).GetHashCode();

            var fileName = $"{Math.Abs(hashCode)}_{counter++}.jpg";

            return  Path.Combine(_root, fileName);


        }
        

        public static ObservableCollection<string> AddKey(string path)
        {
            if (!_dict.ContainsKey(path))
            {
                _dict[path] = new ObservableCollection<string>();
            }
            return _dict[path];
        }

        public static ObservableCollection<string> GetThumbnails(string path)
        {

            if (!_dict.ContainsKey(path))
            {
                throw new KeyNotFoundException();
            }
            return _dict[path];

        }

        
    }
}
